<template>
	<view class="bg">
		<top_nav_return_green :topNavTitle="topNavTitle"></top_nav_return_green>
		<view class="search_box">
			<img src="../../../static/icon/search.png" alt="">
			<input type="text" placeholder="搜索收藏关键词" v-model="searchText">
			<view class="search_box_btn" @click="list_collection(num_a, 'search')"> 搜索 </view>
		</view>
		<!-- 导航栏 -->
		<view class="tabber_box" v-if="list.length>0">
			<view class="tabber_item" :class="{active_tabber_item : num_a == 0}" @tap="num_a_change(0)"> 全部 </view>
			<view class="tabber_item" :class="{active_tabber_item : num_a == item.liftingType}"
				@tap="num_a_change(item.liftingType)" v-for="(item,index) in typeList" :key="index">
				<span v-if="item.liftingType == 1">单选题</span>
				<span v-else-if="item.liftingType == 2">多选题</span>
				<span v-else-if="item.liftingType == 3">共用题干</span>
				<span v-else-if="item.liftingType == 4">共用选项</span>
			</view>
		</view>
		<!-- 收藏列表 -->
		<view class="list_box" v-if="list.length>0">
			<view class="list" v-for="(item,index) in list" :key="index">
				<view class="list_content" @click.stop="queryStudentBySubjectId(item)">
					<view class="list_content_No">
						<span v-if="item.liftingType == 1">单选题</span>
						<span v-else-if="item.liftingType == 2">多选题</span>
						<span v-else-if="item.liftingType == 3">共用题干</span>
						<span v-else-if="item.liftingType == 4">共用选项</span>
					</view>
					{{item.stem}}
				</view>
				<view class="list_text">
					<view class="list_time"> {{item.createTime}} </view>
					<view class="list_btn" @click.stop="delete_collection(item)"> <img
							src="../../../static/icon/collection_ed.png" alt="">移除 </view>
				</view>
			</view>
		</view>
		<view class="censiw" v-else-if="list.length==0 && isAss">
			<image src="../../../static/xing/sc.png" class="zaws" mode=""></image>
			<view class="zanwubiji">
				暂无收藏
			</view>
		</view>
		<view class="list_end" v-if="agree_loding == false&&list.length>0"> —— 到底了 —— </view>
		<!-- 消灭错题 -->
		<!-- 		<view class="footer">
			<view class="footer_tip"> 不要让错题越来越多！ </view>
			<view class="footer_btn" @tap="go_kill()"> 消灭错题 </view>
		</view> -->
		<view class="xmct_btn" v-if="list.length>0" @click="go_kill()">
			智能练习
		</view>
	</view>
</template>

<script>
	import {
		list_collection,
		type_collection,
		delete_collection,
		add_collection,
		practice_collection,
		queryById_collection
	} from '@/api/nav/shoucang.js'
	import {
		queryStudentBySubjectId, //查看原题
	} from '@/api/nav/biji.js'
	import {
		getToken,
		getMajorInfo,
		getUserInfo
	} from '@/common/util/index.js'
	export default {
		data() {
			return {
				topNavTitle: '我的收藏',
				imageUrls: getApp().globalData.imaeWd,
				searchText: '',
				pageNo: 1,
				pageSize: 10,
				typeList: [],
				list: [],
				isAss: false,
				num_a: 0, // 选项卡状态
				agree_loding: true, // 加载状态，默认true可以触底获取数据，
			}
		},
		onShow() {
			console.log('onshow');
			this.list = []
			// this.num_a = 0
			console.log(this.num_a)
			this.type_collection()
			if (this.num_a != 0) {
				this.list_collection(this.num_a, 'search')
			} else {
				this.list_collection(0)
			}
		},
		onReachBottom() {
			console.log('onReachBottom');
			if (this.agree_loding == true) {
				this.pageNo++
				this.list_collection(this.num_a)
			}
		},
		methods: {
			queryStudentBySubjectId(item) {
				console.log(item, 'ww');
				queryStudentBySubjectId({
					majorId: getMajorInfo.id,
					subjectId: item.id,
					type: 1, //0错题1其他
				}).then((res) => {
					console.log('收藏查询原题：', res);
					uni.navigateTo({
						url: `/pagesB/parsing/index?sourceType=99&sourType=1&couti=1&isAss=true&listArr=${encodeURIComponent(JSON.stringify( res.result.list))}`
					})
				})
			},
			go_kill() {
				uni.navigateTo({
					url: '/pagesA/nav/aiQuestionGroup/aiQuestionGroup?agreeChangeFrom=false&num_from=4'
				})
			},
			backPage() {
				uni.reLaunch({
					url: '/pages/index/index'
				})
			},
			type_collection() {
				type_collection({
					majorId: getMajorInfo().id,
				}).then((res) => {
					console.log('获取收藏类型，返回值：', res);
					let list = res.result
					let arr = list.filter((item) => item != null)
					arr.forEach((item, index) => {
						item.liftingType = Number(item.liftingType)
					})
					var compare = function(prop) {
						return function(obj1, obj2) {
							var val1 = obj1[prop];
							var val2 = obj2[prop];
							if (val1 < val2) {
								return -1;
							} else if (val1 > val2) {
								return 1;
							} else {
								return 0;
							}
						}
					}
					arr.sort(compare('liftingType'))
					console.log(arr, '类型排序');
					this.typeList = arr
				})
			},
			list_collection(liftingType, text) {
				
				if (text == 'search') {
					uni.pageScrollTo({
						scrollTop: 0,
						duration: 100
					});
					console.log('搜索关键字、筛选题型');
					this.pageNo = 1
					if (liftingType == 0) {
						list_collection({
							// agencyId: getMajorInfo().agencyId,
							keyWord: this.searchText,
							majorId: getMajorInfo().id,
							pageNo: this.pageNo,
							pageSize: this.pageSize,
							studentId: getUserInfo().id,
							topicCategory: ''
						}).then((res) => {
							this.list = []
							this.list = this.list.concat(res.result.records)
							this.isAss = true
							console.log('全题型搜索', this.list);
							if (res.result.records.length == this.pageSize) {
								this.agree_loding = true
							} else {
								this.agree_loding = false
							}
						})
					} else {
						list_collection({
							// agencyId: getMajorInfo().agencyId,
							keyWord: this.searchText,
							liftingType: liftingType,
							majorId: getMajorInfo().id,
							pageNo: this.pageNo,
							pageSize: this.pageSize,
							studentId: getUserInfo().id,
							topicCategory: ''
						}).then((res) => {
							this.list = []
							this.list = this.list.concat(res.result.records)
							this.isAss = true
							console.log('单题型搜索', this.list);
							if (res.result.records.length == this.pageSize) {
								this.agree_loding = true
							} else {
								this.agree_loding = false
							}
						})
					}
				} else {
					console.log('进入页面，触底加载');
					if (liftingType == 0) {
						list_collection({
							keyWord: this.searchText,
							majorId: getMajorInfo().id,
							pageNo: this.pageNo,
							pageSize: this.pageSize,
							studentId: getUserInfo().id,
							topicCategory: ''
						}).then((res) => {
							this.list = this.list.concat(res.result.records)
							this.isAss = true
							console.log('e', this.list);
							if (res.result.records.length == this.pageSize) {
								this.agree_loding = true
							} else {
								this.agree_loding = false
							}
						})
					} else {
						let data = {
							keyWord: this.searchText,
							liftingType: liftingType,
							majorId: getMajorInfo().id,
							pageNo: this.pageNo,
							pageSize: this.pageSize,
							studentId: getUserInfo().id,
							topicCategory: ''
						}
						console.log('reqdata:', data);
						list_collection(data).then((res) => {
							// console.log('收藏列表获取：', res);
							this.list = this.list.concat(res.result.records)
							this.isAss = true
							console.log('f', this.list);
							if (res.result.records.length == this.pageSize) {
								this.agree_loding = true
							} else {
								this.agree_loding = false
							}
						})
					}
				}
			},
			delete_collection(recieve) {
				console.log('传递来的数据:', recieve);
				let that = this
				uni.showModal({
					title: '移除收藏',
					success: function(res) {
						if (res.confirm) {
							let wdQuestionCollect = {
								agencyId: getMajorInfo().agencyId,
								majorId: getMajorInfo().id,
								subjectId: recieve.id, //题干id
								studentId: getUserInfo().id
							}
							delete_collection(wdQuestionCollect).then((res) => {
								console.log('取消收藏：', res);
								if (res.code == 200) {
									uni.showToast({
										title: '移除成功'
									})
									let index = that.list.findIndex((item, i) => {
										return item.id == recieve.id
									})
									that.list.splice(index, 1)
								} else {
									uni.showToast({
										title: '移除失败',
										icon: 'error'
									})
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			num_a_change(e) {
				this.num_a = e
				this.list_collection(e, 'search')
			},
		}
	}
</script>

<style lang="scss" scoped>
	.censiw {
		display: flex;
		justify-content: center;
		flex-direction: column;
		align-items: center;
	}

	.zaws {
		width: 600rpx;
		height: 600rpx;
	}

	.zanwubiji {
		font-size: 32rpx;
		font-weight: 500;
		color: rgba(102, 102, 102, 0.5);
		text-align: center;
	}

	.list_end {
		padding: 50rpx 0;
		height: 50rpx;
		line-height: 50rpx;
		text-align: center;
		color: #ccc;
		font-size: 30rpx;
	}

	.xmct_btn {
		position: fixed;
		bottom: 30rpx;
		left: 50%;
		margin-left: -250rpx;
		width: 500rpx;
		height: 82rpx;
		text-align: center;
		line-height: 82rpx;
		background: #0191A0;
		border-radius: 40rpx;
		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
	}

	.bg {
		padding-top: 160rpx;
		padding-bottom: 140rpx;
		background: rgba(249, 249, 249, 0.8);
		min-height: 100vh;

	}


	.tabber_box {
		display: flex;
		margin: 33rpx 30rpx;

		.tabber_item {
			margin-right: 10rpx;
			width: 135px;
			height: 51rpx;
			line-height: 51rpx;
			text-align: center;
			background: #EEEEEE;
			border-radius: 26rpx;
			font-size: 24rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #999999;
		}
	}

	.list_box {
		margin: 0 30rpx;

		.list {
			background-color: white;
			padding: 30rpx 30rpx;
			margin-bottom: 30rpx;
			border-radius: 20rpx;


		}
	}

	.list_content {

		.list_content_No {
			display: inline-block;
			margin-right: 30rpx;
			width: 98rpx;
			height: 38rpx;
			line-height: 38rpx;
			text-align: center;
			align-items: center;
			background: #edf7f8;
			border-radius: 10rpx;
			font-size: 24rpx;
			font-weight: bold;
			color: #0492A1
		}
	}

	.list_text {
		margin-top: 33rpx;
		display: flex;
		justify-content: space-between;
		font-size: 24rpx;
		font-family: PingFang SC;
		font-weight: 400;
		color: #999999;

		.list_time {}

		.list_btn {
			display: flex;
			align-items: center;

			img {
				margin-right: 15rpx;
				width: 26rpx;
				height: 26rpx;
			}
		}
	}




	.footer {
		position: fixed;
		bottom: 0;
		background-color: white;
		width: 100vw;
		display: flex;
		justify-content: space-between;
		align-items: center;

		.footer_tip {
			margin-left: 30rpx;
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #132B2E;
		}

		.footer_btn {
			margin: 25rpx 30rpx 25rpx 0;
			width: 270rpx;
			height: 80rpx;
			text-align: center;
			line-height: 80rpx;
			background: #0191A0;
			border-radius: 10rpx;
			font-size: 32rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #FFFFFF;
		}
	}




	.search_box {
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin: 0 30rpx;
		margin-top: 30rpx;
		padding-left: 22rpx;
		padding-right: 10rpx;
		width: 690rpx;
		height: 70rpx;
		background: #FFFFFF;
		border: 1rpx solid #0191A0;
		border-radius: 35rpx;
		background: #fff;

		input {
			width: 70%;
		}

		img {
			width: 34rpx;
			height: 34rpx;
			margin-right: 20rpx;
		}

		.search_box_btn {
			width: 121rpx;
			height: 51rpx;
			line-height: 51rpx;
			text-align: center;
			background: #0191A0;
			border-radius: 26rpx;
			font-size: 24rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #FFFFFF;
		}
	}

	// -----------------------------------------------
	.active_tabber_item {
		width: 135px;
		height: 51rpx;
		line-height: 51rpx;
		text-align: center;
		background: #0191A0 !important;
		border-radius: 26rpx;
		font-size: 24rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #FFFFFF !important;
	}
</style>
